/*
 * File: current_ctl_pi.c
 *
 * Code generated for Simulink model 'current_ctl_pi'.
 *
 * Model version                  : 4.8
 * Simulink Coder version         : 24.1 (R2024a) 19-Nov-2023
 * C/C++ source code generated on : Mon Jun 16 13:49:50 2025
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives:
 *    1. Execution efficiency
 *    2. Traceability
 * Validation result: Not run
 */

#include "current_ctl_pi.h"
#include "rtwtypes.h"
#include <math.h>
#include "mw_cmsis.h"

/* Macros for accessing real-time model data structure */
#ifndef rtmGetcurrent_ctl_pi_InstP_ref
#define rtmGetcurrent_ctl_pi_InstP_ref(rtm) ((rtm)->current_ctl_pi_InstP_ref)
#endif

#ifndef rtmSetcurrent_ctl_pi_InstP_ref
#define rtmSetcurrent_ctl_pi_InstP_ref(rtm, val) ((rtm)->current_ctl_pi_InstP_ref = (val))
#endif

#ifndef rtmGetErrorStatus
#define rtmGetErrorStatus(rtm)         (*((rtm)->errorStatus))
#endif

#ifndef rtmSetErrorStatus
#define rtmSetErrorStatus(rtm, val)    (*((rtm)->errorStatus) = (val))
#endif

#ifndef rtmGetErrorStatusPointer
#define rtmGetErrorStatusPointer(rtm)  (rtm)->errorStatus
#endif

#ifndef rtmSetErrorStatusPointer
#define rtmSetErrorStatusPointer(rtm, val) ((rtm)->errorStatus = (val))
#endif

current_ctl_pi_P current_ctl_pi_rt_P =
{
    /* Computed Parameter: Gain1_Gain
     * Referenced by: '<S2>/Gain1'
     */
    0.666666687F,

    /* Computed Parameter: Constant4_Value
     * Referenced by: '<S3>/Constant4'
     */
    0.0F,

    /* Computed Parameter: us_gainval
     * Referenced by: '<S7>/1//s'
     */
    5.0E-5F,

    /* Computed Parameter: us_IC
     * Referenced by: '<S7>/1//s'
     */
    0.0F,

    /* Computed Parameter: us_gainval_n
     * Referenced by: '<S8>/1//s'
     */
    5.0E-5F,

    /* Computed Parameter: us_IC_i
     * Referenced by: '<S8>/1//s'
     */
    0.0F,

    /* Computed Parameter: Gain1_Gain_c
     * Referenced by: '<S4>/Gain1'
     */
    1.0F
};

/* System initialize for referenced model: 'current_ctl_pi' */
void current_ctl_pi_Init(current_ctl_pi_DW_f *localDW)
{
    /* SystemInitialize for Atomic SubSystem: '<Root>/current_ctl_pi' */
    /* InitializeConditions for DiscreteIntegrator: '<S7>/1//s' */
    localDW->id_ctl_i_sum = current_ctl_pi_rt_P.us_IC;

    /* InitializeConditions for DiscreteIntegrator: '<S8>/1//s' */
    localDW->iq_ctl_i_sum = current_ctl_pi_rt_P.us_IC_i;

    /* End of SystemInitialize for SubSystem: '<Root>/current_ctl_pi' */
}

/* Output and update for referenced model: 'current_ctl_pi' */
void current_ctl_piTID0(void)
{
}

/* Output and update for referenced model: 'current_ctl_pi' */
void current_ctl_piTID1(current_ctl_pi_RT_MODEL * const current_ctl_pi_rt_M,
                        const real32_T rtu_idq_tar_A[2], const real32_T
                        rtu_iabc_now_A[3], const real32_T *rtu_elec_theta_rad,
                        real32_T rty_sv_uabc_V[3], real32_T rty_idq_now_A[2],
                        current_ctl_pi_DW_f *localDW)
{
    current_ctl_pi_InstP *current_ctl_pi_InstP_arg =
        current_ctl_pi_rt_M->current_ctl_pi_InstP_ref;
    real32_T b_vmax;
    real32_T b_vmin;
    real32_T rtb_Gain1_idx_0;
    real32_T rtb_Gain1_idx_1;
    real32_T rtb_Gain1_l_idx_0;
    real32_T rtb_Gain1_l_idx_1;
    real32_T rtb_Gain1_l_idx_2;
    real32_T rtb_Gain1_tmp;
    real32_T rtb_Gain1_tmp_0;
    real32_T rtb_Saturation;
    real32_T rtb_Saturation1;
    real32_T rtb_bw;
    real32_T rtb_bw_h;
    real32_T u1_tmp;
    real32_T u2_tmp;
    real32_T uq_max;

    /* Outputs for Atomic SubSystem: '<Root>/current_ctl_pi' */
    /* Fcn: '<S2>/d' incorporates:
     *  Fcn: '<S4>/a'
     */
    b_vmax = sinf(*rtu_elec_theta_rad + 1.57079637F);

    /* Fcn: '<S2>/q' incorporates:
     *  Fcn: '<S4>/a'
     */
    b_vmin = cosf(*rtu_elec_theta_rad + 1.57079637F);

    /* Fcn: '<S2>/d' incorporates:
     *  Fcn: '<S4>/b'
     */
    rtb_Gain1_l_idx_1 = sinf((*rtu_elec_theta_rad + 1.57079637F) - 2.09439516F);

    /* Fcn: '<S2>/q' incorporates:
     *  Fcn: '<S4>/b'
     */
    rtb_Gain1_tmp = cosf((*rtu_elec_theta_rad + 1.57079637F) - 2.09439516F);

    /* Fcn: '<S2>/d' incorporates:
     *  Fcn: '<S4>/c'
     */
    rtb_Gain1_l_idx_2 = sinf((*rtu_elec_theta_rad + 1.57079637F) + 2.09439516F);

    /* Fcn: '<S2>/q' incorporates:
     *  Fcn: '<S4>/c'
     */
    rtb_Gain1_tmp_0 = cosf((*rtu_elec_theta_rad + 1.57079637F) + 2.09439516F);

    /* Gain: '<S2>/Gain1' incorporates:
     *  Fcn: '<S2>/d'
     *  Fcn: '<S2>/q'
     */
    rtb_Gain1_idx_0 = ((b_vmax * rtu_iabc_now_A[0] + rtb_Gain1_l_idx_1 *
                        rtu_iabc_now_A[1]) + rtb_Gain1_l_idx_2 * rtu_iabc_now_A
                       [2]) * current_ctl_pi_rt_P.Gain1_Gain;
    rtb_Gain1_idx_1 = ((b_vmin * rtu_iabc_now_A[0] + rtb_Gain1_tmp *
                        rtu_iabc_now_A[1]) + rtb_Gain1_tmp_0 * rtu_iabc_now_A[2])
        * current_ctl_pi_rt_P.Gain1_Gain;

    /* Gain: '<S7>/bw' incorporates:
     *  Gain: '<S7>/Gain'
     *  Gain: '<S8>/bw'
     *  Sum: '<S3>/Add'
     */
    rtb_bw_h = current_ctl_pi_InstP_arg->fs * 0.5F * 0.7F * 0.01F;
    rtb_bw = (rtu_idq_tar_A[0] - rtb_Gain1_idx_0) * current_ctl_pi_InstP_arg->bw
        * rtb_bw_h;

    /* Sum: '<S7>/Add' incorporates:
     *  DiscreteIntegrator: '<S7>/1//s'
     *  Gain: '<S7>/kp'
     */
    rtb_Saturation = current_ctl_pi_InstP_arg->ld * rtb_bw +
        localDW->id_ctl_i_sum;

    /* Saturate: '<S7>/Saturation' incorporates:
     *  DiscreteIntegrator: '<S7>/1//s'
     *  DiscreteIntegrator: '<S8>/1//s'
     *  Saturate: '<S3>/Saturation'
     *  Saturate: '<S3>/Saturation1'
     *  Saturate: '<S8>/Saturation'
     */
    u1_tmp = -current_ctl_pi_InstP_arg->dc_bus_limit * 0.5F;
    u2_tmp = current_ctl_pi_InstP_arg->dc_bus_limit * 0.5F;
    if (rtb_Saturation > u2_tmp)
    {
        rtb_Saturation = u2_tmp;
    }
    else if (rtb_Saturation < u1_tmp)
    {
        rtb_Saturation = u1_tmp;
    }

    /* End of Saturate: '<S7>/Saturation' */

    /* Saturate: '<S3>/Saturation' */
    if (rtb_Saturation > u2_tmp)
    {
        rtb_Saturation = u2_tmp;
    }
    else if (rtb_Saturation < u1_tmp)
    {
        rtb_Saturation = u1_tmp;
    }

    /* Gain: '<S8>/bw' incorporates:
     *  Gain: '<S8>/Gain'
     *  Sum: '<S3>/Add1'
     */
    rtb_bw_h *= (rtu_idq_tar_A[1] - rtb_Gain1_idx_1) *
        current_ctl_pi_InstP_arg->bw;

    /* Sum: '<S8>/Add' incorporates:
     *  DiscreteIntegrator: '<S8>/1//s'
     *  Gain: '<S8>/kp'
     */
    rtb_Saturation1 = current_ctl_pi_InstP_arg->lq * rtb_bw_h +
        localDW->iq_ctl_i_sum;

    /* Saturate: '<S8>/Saturation' */
    if (rtb_Saturation1 > u2_tmp)
    {
        rtb_Saturation1 = u2_tmp;
    }
    else if (rtb_Saturation1 < u1_tmp)
    {
        rtb_Saturation1 = u1_tmp;
    }

    /* Saturate: '<S3>/Saturation1' */
    if (rtb_Saturation1 > u2_tmp)
    {
        rtb_Saturation1 = u2_tmp;
    }
    else if (rtb_Saturation1 < u1_tmp)
    {
        rtb_Saturation1 = u1_tmp;
    }

    /* MATLAB Function: '<S3>/MATLAB Function' incorporates:
     *  Constant: '<S3>/Constant'
     */
    /* MATLAB Function 'current_ctl_pi/current_ctl_pi/MATLAB Function': '<S6>:1' */
    /* '<S6>:1:3' uq_max = sqrt(v_out_limit^2-ud_tar^2); */
    rtb_Gain1_l_idx_0 = current_ctl_pi_InstP_arg->dc_bus_limit *
        current_ctl_pi_InstP_arg->dc_bus_limit - rtb_Saturation * rtb_Saturation;
    mw_arm_sqrt_f32(&rtb_Gain1_l_idx_0, &uq_max, 1U);

    /* '<S6>:1:5' if(uq_tar > uq_max) */
    if (rtb_Saturation1 > uq_max)
    {
        /* '<S6>:1:6' uq_tar = uq_max; */
        rtb_Saturation1 = uq_max;
    }
    else if (rtb_Saturation1 < -uq_max)
    {
        /* '<S6>:1:7' elseif(uq_tar < -uq_max) */
        /* '<S6>:1:8' uq_tar = -uq_max; */
        rtb_Saturation1 = -uq_max;
    }

    /* Gain: '<S4>/Gain1' incorporates:
     *  Constant: '<S3>/Constant4'
     *  Fcn: '<S4>/a'
     *  Fcn: '<S4>/b'
     *  Fcn: '<S4>/c'
     *  MATLAB Function: '<S3>/MATLAB Function'
     */
    /* '<S6>:1:10' uq = single(uq_tar); */
    /* '<S6>:1:11' ud = single(ud_tar); */
    rtb_Gain1_l_idx_0 = ((b_vmax * rtb_Saturation + b_vmin * rtb_Saturation1) +
                         current_ctl_pi_rt_P.Constant4_Value) *
        current_ctl_pi_rt_P.Gain1_Gain_c;
    rtb_Gain1_l_idx_1 = ((rtb_Gain1_l_idx_1 * rtb_Saturation + rtb_Gain1_tmp *
                          rtb_Saturation1) + current_ctl_pi_rt_P.Constant4_Value)
        * current_ctl_pi_rt_P.Gain1_Gain_c;
    rtb_Gain1_l_idx_2 = ((rtb_Gain1_l_idx_2 * rtb_Saturation + rtb_Gain1_tmp_0 *
                          rtb_Saturation1) + current_ctl_pi_rt_P.Constant4_Value)
        * current_ctl_pi_rt_P.Gain1_Gain_c;

    /* MATLAB Function: '<S1>/svpwm ' */
    /* MATLAB Function 'current_ctl_pi/svpwm ': '<S5>:1' */
    /* '<S5>:1:3' va = single(uabc(1)); */
    /* '<S5>:1:4' vb = single(uabc(2)); */
    /* '<S5>:1:5' vc = single(uabc(3)); */
    /* '<S5>:1:6' sv_uabc = single(zeros(1,3)); */
    /* '<S5>:1:8' if va>vb */
    if (rtb_Gain1_l_idx_0 > rtb_Gain1_l_idx_1)
    {
        /* '<S5>:1:9' vmax = va; */
        b_vmax = rtb_Gain1_l_idx_0;

        /* '<S5>:1:10' vmin = vb; */
        b_vmin = rtb_Gain1_l_idx_1;
    }
    else
    {
        /* '<S5>:1:11' else */
        /* '<S5>:1:12' vmax = vb; */
        b_vmax = rtb_Gain1_l_idx_1;

        /* '<S5>:1:13' vmin = va; */
        b_vmin = rtb_Gain1_l_idx_0;
    }

    /* '<S5>:1:16' if vc>vmax */
    if (rtb_Gain1_l_idx_2 > b_vmax)
    {
        /* '<S5>:1:17' vmax = vc; */
        b_vmax = rtb_Gain1_l_idx_2;
    }
    else if (rtb_Gain1_l_idx_2 < b_vmin)
    {
        /* '<S5>:1:18' elseif vc<vmin */
        /* '<S5>:1:19' vmin = vc; */
        b_vmin = rtb_Gain1_l_idx_2;
    }

    /* '<S5>:1:22' vcom = 0.5*(vmax+vmin); */
    b_vmax = (b_vmax + b_vmin) * 0.5F;

    /* '<S5>:1:24' ua = va-vcom; */
    rty_sv_uabc_V[0] = rtb_Gain1_l_idx_0 - b_vmax;

    /* '<S5>:1:25' ub = vb-vcom; */
    rty_sv_uabc_V[1] = rtb_Gain1_l_idx_1 - b_vmax;

    /* '<S5>:1:26' uc = vc-vcom; */
    rty_sv_uabc_V[2] = rtb_Gain1_l_idx_2 - b_vmax;

    /* End of MATLAB Function: '<S1>/svpwm ' */

    /* Update for DiscreteIntegrator: '<S7>/1//s' incorporates:
     *  Gain: '<S7>/ki'
     */
    /* '<S5>:1:28' sv_uabc(1) = single(ua); */
    /* '<S5>:1:29' sv_uabc(2) = single(ub); */
    /* '<S5>:1:30' sv_uabc(3) = single(uc); */
    localDW->id_ctl_i_sum += current_ctl_pi_InstP_arg->r * rtb_bw *
        current_ctl_pi_rt_P.us_gainval;
    if (localDW->id_ctl_i_sum > current_ctl_pi_InstP_arg->dc_bus_limit * 0.5F)
    {
        localDW->id_ctl_i_sum = u2_tmp;
    }
    else if (localDW->id_ctl_i_sum < u1_tmp)
    {
        localDW->id_ctl_i_sum = u1_tmp;
    }

    /* Update for DiscreteIntegrator: '<S8>/1//s' incorporates:
     *  Gain: '<S8>/ki'
     */
    localDW->iq_ctl_i_sum += current_ctl_pi_InstP_arg->r * rtb_bw_h *
        current_ctl_pi_rt_P.us_gainval_n;
    if (localDW->iq_ctl_i_sum > current_ctl_pi_InstP_arg->dc_bus_limit * 0.5F)
    {
        localDW->iq_ctl_i_sum = u2_tmp;
    }
    else if (localDW->iq_ctl_i_sum < u1_tmp)
    {
        localDW->iq_ctl_i_sum = u1_tmp;
    }

    /* End of Outputs for SubSystem: '<Root>/current_ctl_pi' */

    /* SignalConversion generated from: '<Root>/idq_now_A' */
    rty_idq_now_A[0] = rtb_Gain1_idx_0;
    rty_idq_now_A[1] = rtb_Gain1_idx_1;
}

/* Model initialize function */
void current_ctl_pi_initialize(const char_T **rt_errorStatus,
    current_ctl_pi_RT_MODEL *const current_ctl_pi_rt_M)
{
    /* Registration code */

    /* initialize error status */
    rtmSetErrorStatusPointer(current_ctl_pi_rt_M, rt_errorStatus);
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
